/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.lastproject;

import Server.MyDataType.Attribute_Value;
import Server.MyDataType.MyRowData;
import Server.MyDataType.MyTable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author CANG
 */
public class MyPopupMenu_RowData extends JPopupMenu
{
    private JMenuItem[] _jmiItems;
    private ActionListener _acl;
    private String _sNameTableOwner;
    private JTable _jTableData;
    private ServerForm _server;
    
    public MyPopupMenu_RowData(JTable jTableData, ServerForm server, String NameTable)
    {
        _jTableData = jTableData;
        _server = server;
        _sNameTableOwner = NameTable;
        _acl = new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                String command = e.getActionCommand();
                char c = command.charAt(0);
                
                switch(c)
                {
                    case 'U':
                    {
                        
                        Vector<MyRowData> vtRowData = new Vector<MyRowData>();
                        TableModel model = _jTableData.getModel();
                        int iRow = _jTableData.getRowCount();
                        int iColumn = _jTableData.getColumnCount();
                        for(int i = 0; i < iRow; i++)
                        {
                            boolean bFillFull = true;
                            
                            MyRowData rowData = new MyRowData();
                            for(int j = 0; j < iColumn; j++)
                            {
                                Object ob = _jTableData.getValueAt(i, j);
                                if(ob == null)
                                {
                                    bFillFull = false;
                                    break;
                                }
                                rowData._vtRow.add(ob.toString());
                            }
                            
                            if(bFillFull)
                            {
                                int iAmountColumnModel = model.getColumnCount();
                                Object ob_ID = model.getValueAt(i, iAmountColumnModel - 1);
                                if(ob_ID != null)
                                {
                                    rowData.ID = ob_ID.toString();                            
                                }
                                vtRowData.add(rowData);
                            }
                        }
                        boolean bUpdate = _server.updateDataRow(_sNameTableOwner, vtRowData);
                        if(!bUpdate)
                        {
                            JOptionPane.showMessageDialog(_jTableData, "Update không thành công", "Thông báo", JOptionPane.ERROR_MESSAGE, null );
                        }
                        else
                        {
                             JOptionPane.showMessageDialog(_jTableData, "Update thành công");
                        }                        
                        break;
                    }
                     
                    case 'D':
                    {
                        
                        boolean bDelete = deleteRow();
                        //thông báo cho người dùng
                        break;
                    }
                        
                    case 'I':
                    {
                        TableModel model = _jTableData.getModel();
                        int iAmountColumn = _jTableData.getColumnCount();
                        String[] sNewData = new String[iAmountColumn];
                        ((DefaultTableModel)model).addRow(sNewData);                        
                        break;
                    }
                            
                }
                
            }
        };
        
        
        _jmiItems = new JMenuItem[3];        
        _jmiItems[0] = new JMenuItem("Update Data");
        _jmiItems[2] = new JMenuItem("Delete Data Row");
        _jmiItems[1] = new JMenuItem("Insert Data Row");
        
        for(int i = 0; i < _jmiItems.length; i++)
        {
            add(_jmiItems[i]);      
            _jmiItems[i].addActionListener(_acl);
        }        
    }
    
    public void setNameTableOwner(String nameTable)
    {
        _sNameTableOwner = nameTable;
    }
    
    private boolean deleteRow()
    {        
        boolean bDelete = false;
        MyRowData rowData = new MyRowData();
        DefaultTableModel _dtmTable = (DefaultTableModel)_jTableData.getModel();
        int indexSelect = _jTableData.getSelectedRow();
        int iAmountColumn = _jTableData.getColumnCount();
        if(indexSelect >= 0)
        {
            String value = "null";
            for(int i = 0; i < iAmountColumn; i++)
            {
               String svalueTemp =  _jTableData.getValueAt(indexSelect, i).toString();
               if(svalueTemp.equals(""))
                   svalueTemp = "null";
               
               value = svalueTemp;
               rowData._vtRow.add(value);               
            }
            
            TableModel model = (TableModel)_jTableData.getModel();
            int iAmountColumnModel = model.getColumnCount();
            String iDRow = model.getValueAt(indexSelect, iAmountColumnModel - 1).toString();
            bDelete = _server.deleteDataRow(_sNameTableOwner, iDRow);
            if(bDelete)
            {
                _dtmTable.removeRow(indexSelect);
            }
            
        }
        return bDelete;
    }
}
